/**
 * 
 */
package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.tools.ant.util.DateUtils;

import com.hexacta.hrs.domain.staffing.Project;
import com.hexacta.hrs.domain.staffing.relationship.RequirementAssignmentRelationship;
import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.StaffingPendingRequirementsReportDTO;
import com.hexacta.hrs.service.staffing.ProjectService;

/**
 * @author iandronowicz clase que genera el reporte general de timeoff
 */
public class StaffingPendingRequirementsReport extends AbstractReport {

    private ProjectService projectService;

    private Date selectedDateFrom;

    /**
     * @return the projectService
     */
    public ProjectService getProjectService() {
        return projectService;
    }

    /**
     * @param projectService
     *            the projectService to set
     */
    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }

    /**
     * @return the dateFrom
     */
    public Date getDateFrom() {
        return selectedDateFrom;
    }

    /**
     * @param dateFrom
     *            the dateFrom to set
     */
    public void setDateFrom(final Date dateFrom) {
        this.selectedDateFrom = dateFrom;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    protected Collection getReportData(final Map<String, Object> filters) {

        List<StaffingPendingRequirementsReportDTO> collection = new ArrayList<StaffingPendingRequirementsReportDTO>();

        if (filters != null) {
            if (filters.containsKey("selectedDateFrom")) {
                selectedDateFrom = (Date) filters.get("selectedDateFrom");
            }
        }

        List<Project> projects = projectService.findAll();

        for (Project p : projects) {

            List<RequirementAssignmentRelationship> reqs = p.getUnassignedRequirements(true);

            for (RequirementAssignmentRelationship req : reqs) {
                if (selectedDateFrom == null || !selectedDateFrom.before(req.getStartDate().getTime())
                        && !selectedDateFrom.after(req.getEndDate().getTime())) {

                    StaffingPendingRequirementsReportDTO dto = new StaffingPendingRequirementsReportDTO();

                    dto.setClient(p.getClient().getName());
                    dto.setDateFrom(DateUtils.format(req.getStartDate().getTime(), "dd/MM/yyyy"));
                    dto.setProject(p.getName());
                    dto.setSeniority(req.getRole().getDescription());

                    collection.add(dto);
                }
            }
        }

        if (collection.isEmpty())
            throw new RuntimeException("There is no data for this report");

        Collections.sort(collection);

        return collection;
    }

    /**
     * @param req
     * @return
     */

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    protected Map getReportParameters() {
        // TODO Auto-generated method stub
        return null;
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }
}
